/*
 * @since 10 may 2012
 * Settings Dialog
 * @author Koen Ursem
 */
package gui;

import config.Config;
import config.FileRestrictionSelect;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;

/**
 * The settings dialog
 *
 * @author Koen Ursem
 */
public class SettingsDialog extends JDialog implements ActionListener {

    private JLabel labelIp, labelPort;
    private JTextField fieldIp;
    private JTextField fieldPort;
    private JButton buttonSave, buttonClose, buttonBackup, buttonLoad;

    /**
     * Sets the Dialogs frame and builds the form
     *
     * @author Koen Ursem
     * @since 0.1
     */
    public SettingsDialog() {
	//creates the dialog window
	setModalityType(ModalityType.APPLICATION_MODAL);
	setSize(350, 200);
	setLocationRelativeTo(null);
	setLayout(new FlowLayout());
	setTitle("Bunie Settings");

	// loads the form in the dialog
	buildForm();
	setVisible(true);

	// remove this dialog window if clicked on the red marker
	this.addWindowListener(new WindowAdapter() {

	    @Override
	    public void windowClosing(WindowEvent we) {
		setVisible(false);
	    }
	});
    }

    /**
     * Builds the settings form
     *
     * @author Koen Ursem
     * @since 0.1
     */
    private void buildForm() {

	// all items loaded for the form including action listeners
	labelIp = new JLabel("Server Ip");
	add(labelIp);
	fieldIp = new JTextField(20);
	fieldIp.setText(Config.ip);
	add(fieldIp);
	labelPort = new JLabel("Server Port");
	add(labelPort);
	fieldPort = new JTextField(20);
	fieldPort.setText(Integer.toString(Config.port));
	add(fieldPort);

	buttonSave = new JButton("Save");
	buttonSave.addActionListener(this);
	add(buttonSave);

	buttonLoad = new JButton("Load");
	buttonLoad.addActionListener(this);
	add(buttonLoad);

	buttonClose = new JButton("Close");
	buttonClose.addActionListener(this);
	add(buttonClose);

	buttonBackup = new JButton("Backup");
	buttonBackup.addActionListener(this);
	add(buttonBackup);
    }

    /**
     * The button action handling when a button is pressed
     *
     * @author Koen Ursem
     */
    @Override
    public void actionPerformed(ActionEvent e) {
	//listener for all buttons and executed functions
	if (e.getSource() == buttonSave) {
	    performSave();
	}
	if (e.getSource() == buttonClose) {
	    setVisible(false);
	}
	if (e.getSource() == buttonBackup) {
	    performBackup();
	}
	if (e.getSource() == buttonLoad) {
	    performLoad();
	}
    }

    /**
     * Save changes into the configs
     *
     * @author Koen Ursem
     */
    private void performSave() {
	// get the values from the fields 
	String ip = fieldIp.getText();
	String port = fieldPort.getText();
	
	//createa configs file to be able to update the static values of the config class
	Config configs = new Config();

	//add changes to the properties class
	configs.updateConfig("serverip", ip);
	configs.updateConfig("poort", port);

	//save the config settings into the main .configs file so it can be loaded again next time
	configs.saveConfigs();
    }

    /**
     * Creates a backup of the last saved configs (changes that are not saved
     * are not included!)
     *
     * @author Koen Ursem
     */
    private void performBackup() {
	Config configs = new Config();
	configs.backupConfigs();
    }

    /**
     * Creates a backup of the last saved configs (changes that are not saved
     * are not included!)
     *
     * @author Koen Ursem
     */
    private void performLoad() {
	//load the default directory
	String dir = System.getProperty("user.dir");

	//to load file selection rules, home and uper directorys are disabled with it and the creation of new folders aswel
	FileSystemView fsv = new FileRestrictionSelect(new File(dir));

	//create a filechooser to select the file to load
	JFileChooser choose = new JFileChooser(dir, fsv);
	choose.setFileSelectionMode(JFileChooser.FILES_ONLY);
	choose.showDialog(this, "Load config");

	//the selected file
	File f = choose.getSelectedFile();

	try {
	    //file name to load in loadConfigsName
	    String name = f.getName();

	    //debug check
	    System.out.println(name);

	    //creates the config class to call its commands
	    Config configs = new Config();

	    //finaly loads the configs
	    configs.loadConfigsName(name);

	    //reloads the config in the form
	    fieldIp.setText(Config.ip);
	    fieldPort.setText(Integer.toString(Config.port));
	} catch (NullPointerException npe) {
	    System.out.println("User closed the file selector.");
	}
    }
}
